package logic

import (
	"context"
	"errors"
	"lm/models/mysql"

	"lm/lmrpc/internal/svc"
	"lm/lmrpc/lmrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BrandAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBrandAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BrandAddLogic {
	return &BrandAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BrandAddLogic) BrandAdd(in *lmrpc.BrandAddRequest) (*lmrpc.BrandAddResponse, error) {
	// todo: add your logic here and delete this line
	var findBrand mysql.Brand
	mysql.DB.Debug().Where("brandName=?", in.BrandName).Find(&findBrand)
	if findBrand.BrandName == in.BrandName {
		return nil, errors.New("品牌已存在,不允许添加")
	}

	brand := mysql.Brand{
		BrandName: in.BrandName,
	}
	mysql.DB.Debug().Create(&brand)
	return &lmrpc.BrandAddResponse{
		BrandId: int64(brand.ID),
	}, nil
}
